package com.bicon.botu.mall.component.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.bicon.botu.mall.component.common.PageContentConstants;
import com.bicon.botu.mall.component.dto.PageContentDTO;
import com.bicon.botu.mall.component.entity.PageContent;
import com.bicon.botu.mall.component.mapper.PageContentMapper;
import com.bicon.botu.mall.component.service.IPageContentService;
import com.bicon.botu.core.base.common.Constants;
import com.bicon.botu.core.base.common.entity.BaseResult;
import com.bicon.botu.core.base.common.utils.BeanMapper;
import com.bicon.botu.core.base.common.utils.JsonUtil;
import com.bicon.botu.core.base.common.utils.StringUtils;
import com.bicon.botu.core.base.repository.redis.RedisHandler;
import com.bicon.botu.core.base.repository.redis.RedisTemplateUtil;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;


/**
 * 字典后台接口
 *
 * @author 张露晨
 * @since 2017-07-08
 */
@Service
@Transactional
public class IPageContentServiceImpl  extends ServiceImpl<PageContentMapper, PageContent> implements IPageContentService {

    @Autowired
    private RedisHandler redisHandler;

    /**
     * （批量）新增字典内容
     *
     * @param dictionaryCode 字典Code
     * @param title          标题
     * @param author         作者
     * @param content        内容
     * @return
     */
    @Override
    public BaseResult addPageContent(String dictionaryCode, String title, String author, String content) {
        //参数校验
        if (StringUtils.isAnyBlank(dictionaryCode, title, content)) {
            return new BaseResult(Constants.RESPONSE_CODE_400, Constants.RESPONSE_MESSAGE_400);
        }
        PageContent pageContent = new PageContent();
        pageContent.setDictionaryCode(dictionaryCode);
        pageContent.setTitle(title);
        pageContent.setAuthor(author);
        pageContent.setContent(content);
        pageContent.preInsert();
        boolean result = pageContent.insert();
        if (!result) {
            return new BaseResult(Constants.RESPONSE_CODE_600, Constants.RESPONSE_MESSAGE_600);
        }
        redisHandler.getGlobalRedisTemplate().cacheHash(PageContentConstants.PAGE_REDIS_KEY,"",dictionaryCode,content);
        return new BaseResult(Constants.RESPONSE_CODE_200, Constants.RESPONSE_MESSAGE_200);
    }

    /**
     * （批量）逻辑删除字典内容
     *
     * @param idJson 字典id集合
     * @return
     */
    @Override
    public BaseResult del(String idJson) {
        //获取id集合并检查
        ArrayList<String> idList = JsonUtil.fromJson(idJson, ArrayList.class);
        if (CollectionUtils.isEmpty(idList)) {
            return new BaseResult(Constants.RESPONSE_CODE_400, Constants.RESPONSE_MESSAGE_400);
        }
        List<PageContent> pageContentList = selectList(new EntityWrapper<PageContent>().in("id",idList));
        if(CollectionUtils.isNotEmpty(pageContentList)){
            pageContentList.stream().forEach(pageContent -> pageContent.preDelete());
            boolean result = updateBatchById(pageContentList);
            if(!result){
                return new BaseResult(Constants.RESPONSE_CODE_600, Constants.RESPONSE_MESSAGE_600);
            }
        }
        //删除缓存
        RedisTemplateUtil redisTemplateUtil = redisHandler.getGlobalRedisTemplate();
        pageContentList.stream().forEach(pageContent -> {
            redisTemplateUtil.removeHash(PageContentConstants.PAGE_REDIS_KEY,"",pageContent.getDictionaryCode());
        });
        return new BaseResult(Constants.RESPONSE_CODE_200, Constants.RESPONSE_MESSAGE_200);
    }

    /**
     * 修改单页字典内容
     *
     * @param id             主键
     * @param dictionaryCode 字典Code
     * @param title          标题
     * @param author         作者
     * @param content        内容
     * @return
     */
    @Override
    public BaseResult update(String id, String dictionaryCode, String title, String author, String content) {
        //参数校验
        if (StringUtils.isBlank(id)) {
            return new BaseResult(Constants.RESPONSE_CODE_400, Constants.RESPONSE_MESSAGE_400);
        }
        //判断该条记录是否存在
        PageContent pageContent = new PageContent();
        pageContent.setId(id);
        pageContent = pageContent.selectById();
        if (null == pageContent) {
            return new BaseResult(Constants.RESPONSE_CODE_602, Constants.RESPONSE_MESSAGE_602);
        }
        //设置参数
        if (StringUtils.isNoneBlank(dictionaryCode)) {
            pageContent.setDictionaryCode(dictionaryCode);
        }
        if (StringUtils.isNoneBlank(author)) {
            pageContent.setAuthor(author);
        }
        if (StringUtils.isNoneBlank(title)) {
            pageContent.setTitle(title);
        }
        if (StringUtils.isNoneBlank(content)) {
            pageContent.setContent(content);
        }
        //数据库更新
        boolean result = pageContent.updateById();
        if (!result) {
            return new BaseResult(Constants.RESPONSE_CODE_600, Constants.RESPONSE_MESSAGE_600);
        }
        //更新缓存
        redisHandler.getGlobalRedisTemplate().cacheHash(PageContentConstants.PAGE_REDIS_KEY,"",pageContent.getDictionaryCode(),pageContent.getContent());
        return new BaseResult(Constants.RESPONSE_CODE_200, Constants.RESPONSE_MESSAGE_200);
    }

    /**
     * 根据字典code查找字典
     *
     * @param dictionaryCode 字典Code
     * @return
     */
    @Override
    public BaseResult select(String dictionaryCode) {
        //参数校验
        if (StringUtils.isBlank(dictionaryCode)) {
            return new BaseResult(Constants.RESPONSE_CODE_400, Constants.RESPONSE_MESSAGE_400);
        }
        //查询集合
        List<PageContent> pageContentList = new PageContent().selectList(
                new EntityWrapper<PageContent>().eq("dictionary_code", dictionaryCode).eq("logic_delete", Constants.DEL_LOGIC_NORMAL));
        if (null == pageContentList) {
            return new BaseResult(Constants.RESPONSE_CODE_600, Constants.RESPONSE_MESSAGE_600);
        }
        return new BaseResult(Constants.RESPONSE_CODE_200, Constants.RESPONSE_MESSAGE_200, BeanMapper.mapList(pageContentList, PageContentDTO.class));
    }


    /**
     * 根据字典code分页查找字典
     *
     * @param pageNo         页码
     * @param pageSize       每页记录数
     * @param dictionaryCode 字典Code
     * @return
     */
    @Override
    public BaseResult selectPage(String pageNo, String pageSize, String dictionaryCode) {
        //校验参数
        if (StringUtils.isAnyBlank(pageNo, pageNo)) {
            return new BaseResult(Constants.RESPONSE_CODE_400, Constants.RESPONSE_MESSAGE_400);
        }
        int pNo = Integer.parseInt(pageNo);
        int pSize = Integer.parseInt(pageSize);
        EntityWrapper entityWrapper = new EntityWrapper<PageContent>();
        if(StringUtils.isNotEmpty(dictionaryCode)){
            entityWrapper.eq("dictionary_code", dictionaryCode);
        }
        entityWrapper.eq("logic_delete", Constants.DEL_LOGIC_NORMAL);
        //分页查询
        Page<PageContent> pageContentPage = new PageContent().selectPage(
                new Page<>(pNo, pSize),
                entityWrapper);
        return new BaseResult(Constants.RESPONSE_CODE_200, Constants.RESPONSE_MESSAGE_200, pageContentPage);
    }


}
